
/**
 * Camera
 * @version 1.0
 * @since 1.0
 * <p>
 * Created by Murphy at 2021/7/9 17:06
 **/

#pragma once

#include "math/Math.hpp"
#include "math/Ray.hpp"

namespace Stitches
{

class Camera
{
public:
    Camera(Vec3f lookFrom, Vec3f lookAt, float vfov, float aspect_ratio)
    {
//        auto aspect_ratio = 16.0 / 9.0;
        auto theta = degrees_to_radians(vfov);
        auto h = tan(theta / 2);
        float viewport_height = 2.0 * h;
        float viewport_width = aspect_ratio * viewport_height;
        auto focal_length = 1.0;

        Vec3f w = glm::normalize(lookFrom - lookAt);
        Vec3f u = glm::normalize(glm::cross(Vec3f(0, 1, 0), w));
        Vec3f v = cross(w, u);


        mOrigin = Vec3f(0, 0, 0);
        mHorizontal = Vec3f(viewport_width, 0.0, 0.0);
        mVertical = Vec3f(0.0, viewport_height, 0.0);

        float focus = 10.0f;


        mOrigin = lookFrom;
        mHorizontal =  viewport_width * u * focus;
        mVertical = viewport_height * v * focus;

        //视口左下角的坐标
        mLowerLeftCorner = mOrigin - mHorizontal * 0.5f - mVertical * 0.5f - focus * w;

        float aperture = 0.1f;
        mLensRadius = aperture * 0.5f;
    }

    Ray getRay(double u, double v) const;
private:
    Vec3f mOrigin;
    Vec3f mLowerLeftCorner;
    Vec3f mHorizontal;
    Vec3f mVertical;
    float mLensRadius;
};

}

